package cn.zl.mapper;

import cn.yunbamboos.mybatis.annotation.CustomQuery;
import cn.yunbamboos.mybatis.annotation.GenerateXML;
import cn.yunbamboos.mybatis.conditions.Conditions;
import cn.yunbamboos.mybatis.mapper.GenericMapper;
import cn.zl.model.StreamProxy;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@GenerateXML
@Repository
public interface StreamProxyMapper extends GenericMapper<StreamProxy> {

    default Optional<StreamProxy> query(String app,String stream, String url){
        return this.queryForObject(Conditions.create("app", app).add("stream",stream).add("url", url));
    }

    default boolean existAppAndStream(String app, String stream){
        Conditions conditions = Conditions.create("app", app).add("stream", stream);
        Optional<StreamProxy> optional = this.queryForObject(conditions);
        return optional.isPresent();
    }

    @CustomQuery(sql = "select count(1) from zl_stream_proxy", resultType = Integer.class)
    int total();

    @CustomQuery(sql = "select count(1) from zl_stream_proxy where state=#{state}", resultType = Integer.class)
    int countState(@Param("state") int state);

}
